package come.demo.widgets;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by Administrator on 2017/11/30.
 */

public abstract class BasePageAdapter<T, VH extends BaseRecyclerAdapter.BaseViewHolder> extends BaseRecyclerAdapter<T, VH> {

    int mPageNum = 0;
    public static final int PAGE_SIZE = 10;
    public static final int FIRST_PAGE = 1;

    public BasePageAdapter(List<T> data) {
        super(data);
    }

    public int getCurrentPageNum() {
        return mPageNum;
    }


    public int getNextPageNum() {
        return mPageNum + 1;
    }


    @Override
    public void refreshDatas(List<T> data) {
        super.refreshDatas(data);
        updatePageNum();
    }


    /**
     * 如果刷新的有数据，返回true
     * 如果刷新的数据为空，返回false
     */
    public boolean refreshDatasArray(T[] data) {
        checkDataIsError(data);
        mData = new ArrayList<>();
        boolean result = false;
        if (data != null && data.length > 0) {
            Collections.addAll(mData, data);
            result = true;
        }
        updatePageNum();
        notifyItemRangeChanged(0,Integer.MAX_VALUE);
        return result;

    }

    /**
     * 如果添加的有数据，返回true
     * 如果添加的数据为空，返回false
     */
    public boolean addData(T[] data) {
        checkDataIsError(data);
        if (mData == null) {
            mData = new ArrayList<>();
        }
        if (data == null || data.length == 0)
            return false;


        final int start = mData.size();

        Collections.addAll(mData, data);
        updatePageNum();
        notifyInsert(start, data.length);
        return true;


    }

    /**
     * 返回是否还有下一页
     */
    public boolean refreshDataArrayWithHasNextPage(T[] data) {
        checkDataIsError(data);
        mData = new ArrayList<>();
        if (data != null && data.length > 0) {
            Collections.addAll(mData, data);
        }
        updatePageNum();
        notifyDataSetChanged();

        return data == null || data.length >= PAGE_SIZE;
    }

    /**
     * 返回是否还有下一页
     */
    public boolean addDataArrayWithHasNextPage(T[] data) {
        checkDataIsError(data);
        if (mData == null) {
            mData = new ArrayList<>();
        }
        if (data != null && data.length > 0) {

            final int start = mData.size();
            Collections.addAll(mData, data);
            updatePageNum();
            notifyInsert(start, data.length);
        }


        return data == null || data.length >= PAGE_SIZE;
    }

    protected void notifyInsert(int start, int length) {
        notifyItemRangeInserted(start, length);
    }


    public boolean removeData(int pos) {


        T col = mData.remove(pos);
        if (col == null)
            return false;
        updatePageNum();
        notifyItemRemoved(pos);
        return true;
    }

    private int getDataPageNum(List<T> data) {
        if (data == null || data.size() == 0)
            return 0;

        final int sz = data.size();
        if (sz % PAGE_SIZE == 0) {
            return sz / PAGE_SIZE;
        } else {
            return sz / PAGE_SIZE + 1;
        }
    }

    private int updatePageNum() {
        mPageNum = getDataPageNum(mData);
        return mPageNum;
    }


    private int getDataPageNum(T[] data) {
        if (data == null || data.length == 0)
            return 0;


        final int sz = data.length;
        if (sz % PAGE_SIZE == 0) {
            return sz / PAGE_SIZE;
        } else {
            return sz / PAGE_SIZE + 1;
        }
    }

    public ArrayList<T> getData() {
        return (ArrayList<T>) mData;
    }


}
